package com.wwwarehouse.financialcenter.fragment.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomDisViewPager;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.adapter.MyPagerAdapter;

import java.util.ArrayList;

/**
 * @author Joseph on 2017/9/23.
 *         <p/>
 *         ViewPager基类
 */
public class MyBaseViewPagerFragment extends TheParentFragment implements ElasticScrollView.OnPullListener {
    public CustomDisViewPager mCustomPager;
    public CirclePageIndicator mPagerInternal;
    public ElasticScrollView mCustomScroll;
    public Button mConfirmBtn;
    public SlideBarView mSlideBarView;
    public StateButton mStateButton;
    public ArrayList<Fragment> fragmentList;
    public MyPagerAdapter mPagerAdapter;
    public LinearLayout mEmptyLayout;
    public TextView mEmptyTxt;
    public int count = 1;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_base_financial_viewpager_fragment;
    }

    @Override
    protected void init(View view) {
        mEmptyLayout = findView(view, R.id.ll_empty);
        mEmptyTxt = findView(view, R.id.tv_empty);
        mCustomPager = (CustomDisViewPager) view.findViewById(R.id.vp_content);
        mPagerInternal = (CirclePageIndicator) view.findViewById(R.id.pager_indicator);
        mCustomScroll = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
        mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);
        mSlideBarView = findView(view, R.id.sbv_slide);
        mStateButton = (StateButton) view.findViewById(R.id.btn_state);
        mCustomScroll.setOnPullListener(this);
    }

    /**
     * 显示搜索无结果界面
     */
    public void ShowEmptyView() {
        mCustomPager.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.VISIBLE);
        mEmptyTxt.setText(R.string.invite_search_no_result);
    }
    public void ShowEmptyView(CharSequence content) {
        mCustomPager.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.VISIBLE);
        mEmptyTxt.setText(TextUtils.isEmpty(content)?getResources().getString(R.string.invite_search_no_result):content);
    }
    /**
     * 取消按钮的点击事件
     */
    @Override
    public void cancelClick() {
        mCustomPager.setVisibility(View.VISIBLE);
        mEmptyLayout.setVisibility(View.GONE);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    /**
     * @param total   总个数
     * @param mNum    每一页的个数
     * @param mBundle Bundle 自定义数据
     */
    public void SetFragmentAndPage(int total, int mNum, String fname, Bundle mBundle) {
        mConfirmBtn.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mCustomPager.setVisibility(View.VISIBLE);
        count = 1;
        fragmentList = new ArrayList<>();
        if (total > mNum) {
            if ((total - mNum) % mNum == 0) {
                count = (total - mNum) / mNum + 1;
            } else {
                count = (total - mNum) / mNum + 2; // 总共显示多少张卡片
            }
        } else {
            count = 1;
        }
        Bundle bundle = null;
        for (int i = 1; i <= count; i++) {
                bundle = new Bundle();
                bundle.putInt("pos", i);         //当前第几页,从1开始
                bundle.putInt("count", count);   //总页数
                bundle.putInt("num", mNum);      //每一页个数
            if (mBundle!=null){
                bundle.putAll(mBundle);
            }
            Fragment fragment = Fragment.instantiate(mActivity, fname);
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
        }
        if (isAdded()) {
            mPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), fragmentList);
            mCustomPager.setAdapter(mPagerAdapter);
        }
        if (count <= 6) {
            if (count > 1) {
                mPagerInternal.setVisibility(View.VISIBLE);
                mSlideBarView.setVisibility(View.GONE);
                mPagerInternal.setViewPager(mCustomPager, 0);
            } else {
                mPagerInternal.setVisibility(View.GONE);
                mSlideBarView.setVisibility(View.GONE);
            }
        } else {
            mPagerInternal.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.VISIBLE);
            mSlideBarView.setTotalPage(count);
            mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                @Override
                public void onSlideChange(int page) {
                    mCustomPager.setCurrentItem(page, false);
                }
            });
            if (mCustomPager.getAdapter()!=null){
                mSlideBarView.setViewPagerInternal(mCustomPager);
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void setTitle() {

    }

    @Override
    public void onUpPull() {

    }

}